package cn.chenhongchao.zhaobiaopingtai.service.impl;

import cn.chenhongchao.zhaobiaopingtai.common.security.JwtTokenUtil;
import cn.chenhongchao.zhaobiaopingtai.entity.Users;
import cn.chenhongchao.zhaobiaopingtai.entity.UsersExample;
import cn.chenhongchao.zhaobiaopingtai.mapper.UsersMapper;
import cn.chenhongchao.zhaobiaopingtai.service.ISupplierLpginDataService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;


@Service
public class SupplierLoginDataServiceImpl implements ISupplierLpginDataService {

    @Resource
    private UsersMapper usersMapper;
    @Resource
    private PasswordEncoder passwordEncoder;


    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.tokenHead}")
    private String tokenHead;


//    @Override
//    public Boolean loginPhone(String phone, String password) {
//        UsersExample users = new UsersExample();
//        users.createCriteria().andPhoneEqualTo(phone);
//        Users users1 = usersMapper.selectByExample(users).get(0);
////        boolean matches = passwordEncoder.matches(password, users1.getPasswd());
////        System.out.println(matches);
////        System.out.println("users1.getPasswd()=="+users1.getPasswd()+"=="+password);
//        String jwt = null;
//
//        // 客户端加密后传递账号密码
//        try {
//            UserDetails userDetails = (UserDetails) users1;
//
//            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,
//                    null, userDetails.getAuthorities());
//
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//
//            String token = jwtTokenUtil.generateToken(userDetails);
//
//            // 这里需要记录登录时间
//            updateLoginTime(name);
//            logAccountLoginService.insert(name);
//
//            // 注意中间用空格隔开
//            jwt = tokenHead + BLANK_SPACE + token;
//
//        } catch (AuthenticationException e) {
//            LOGGER.warn("user :{} , login fail :{}", name, e.getMessage());
//        }
//
//
//        return Optional.ofNullable(jwt);
//
//        return passwordEncoder.matches(password, users1.getPasswd());
//    }

    @Override
    public Users getUserByPhone(String phone) {
        UsersExample users = new UsersExample();
        users.createCriteria().andPhoneEqualTo(phone);
        Users users1 = usersMapper.selectByExample(users).get(0);
        users1.setPasswd("");
        return users1;
    }

    @Override
    public Boolean login(String supplierName, String password) {
        UsersExample users = new UsersExample();
        users.createCriteria().andPasswdEqualTo(password).andUsernameEqualTo(supplierName);

        if(usersMapper.selectByExample(users).size()==1){
            return  true;
        }
        return false;
    }
}
